#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <error.h>
#include <unistd.h>
#include <string>
using namespace std;

#define BUFFERSIZE 256
void writeToFile(int fd)
{
    /* 向文件写数据 */
    string buf = "hello world!";
    int writeBytes = write(fd, buf.c_str(), buf.size());
    cout << "writeBytes" << writeBytes << endl;
}

void readFromFile(int fd)
{
    char buffer[BUFFERSIZE] = {0};
    int readBytes = read(fd, buffer, sizeof(buffer));
    cout << "readBytes:" << readBytes << " buffer:" << buffer << endl;
}

int main()
{
    const char *filename = "./test1.txt";
    int fd1 = open(filename, O_RDWR | O_CREAT, 0644);
    if (fd1 == -1)
    {
        /* 打印错误原因 */
        perror("error:");
        // std::cout << "file open error" << std::endl;
    }
    cout << "fd1:" << fd1 << endl;

    /* 读取数据 */
    readFromFile(fd1);

    /* 关闭文件 */
    close(fd1);
    return 0;
}